package com.ebupt.service

import android.content.Context
import android.util.Log
import androidx.lifecycle.LiveData
import androidx.paging.DataSource
import androidx.paging.PagingData
import androidx.paging.PagingSource
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

object UserDaoHelper{

    fun addUser(context: Context,user:AppUserInfo){
        GlobalScope.launch(Dispatchers.IO) {
            UserDataBase.getInstance(context).useDao.insertUser(user)
        }

    }


    fun addUserList(context: Context,userList:List<AppUserInfo>){
        GlobalScope.launch(Dispatchers.IO) {
            kotlin.runCatching {
                UserDataBase.getInstance(context).useDao.insertUserList(userList)
            }.onSuccess {
                Log.e("DB","插入成功")
            }.onFailure {
                Log.e("DB","插入失败")
                Log.e("DB",it.message)
            }

        }

    }


    fun deleteUser(context: Context,user: AppUserInfo) = UserDataBase.getInstance(context).useDao.delete(user)


    fun updateUser(context: Context,user: AppUserInfo) = UserDataBase.getInstance(context).useDao.updateUser(user)

    fun queryUser(context: Context):LiveData<List<AppUserInfo>> = UserDataBase.getInstance(context).useDao.getAllUser()


    fun queryUserNomal(context: Context):List<AppUserInfo> = UserDataBase.getInstance(context).useDao.getAllUserNomal()


   // fun queryUserForPaging(context: Context): PagingData<AppUserInfo> = UserDataBase.getInstance(context).useDao.getAllUserForPaging()


}